package v1

import (
	"pq/models"
	//"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego"
)

type SlTestItemMaterialController struct {
	TokenCheckController
}

func (smc *SlTestItemMaterialController) Get(){
	library_id := smc.Ctx.Input.Param(":id")
	sllib := new(models.PqSlTestItemMaterial)
	sls,count := sllib.ListAll(library_id)
	qs := sllib.ListAllQuestion(library_id)
	smc.Response(map[string]interface{}{
		"sl" : sls,
		"qs" : qs,
	},count)
}
func (smc *SlTestItemMaterialController) GetQuestion(){
	library_id := smc.Ctx.Input.Param(":id")
	sllib := new(models.PqSlTestItemMaterial)
	sls := sllib.ListAllQuestion(library_id)
	smc.Response(sls,1)
}

func (smc *SlTestItemMaterialController) Post() {
	row := new (models.PqSlTestItemMaterial)
	row.Id,_  = smc.GetInt("id",0)
	row.Body = smc.GetString("body")
	row.PqSlTestLibraryId,_ = smc.GetInt("pq_sl_test_library_id",0)
	row.Ordering,_ = smc.GetInt("ordering")
	master := models.GetMasterDb()
	id := 0
	//update
	if row.Id > 0 {
		id = row.Id
		_,err := master.Update(row)
		beego.Error(err)
	} else {
		ins, _ := master.Insert(row)
		id = int(ins)
	}
	res := map[string]interface{}{}
	res["id"] = id
	smc.Single(res)
}